package com.forum.controller.admin;

import com.forum.constant.JwtClaimsConstant;
import com.forum.constant.MessageConstant;
import com.forum.dto.UserDTO;
import com.forum.dto.UserForgotDTO;
import com.forum.dto.UserPageQueryDTO;
import com.forum.entity.User;
import com.forum.properties.JwtProperties;
import com.forum.result.PageResult;
import com.forum.result.Result;
import com.forum.service.UserService;
import com.forum.utils.JwtUtil;
import com.forum.vo.UserInfoVO;
import com.forum.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;


@Slf4j
@Api(tags = "用户登录注册接口")
@RestController("AdminUserController")
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;
    @ApiOperation("用户登录接口")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserDTO userDTO){
        log.info("员工登录: {}", userDTO);
        User user = userService.login(userDTO);

        HashMap<String, Object> map = new HashMap<>();
        map.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                map);

        // 通过用户的id，账号，角色生成token
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .username(user.getUsername())
                .role(user.getRole())
                .token(token)
                .name(user.getName())
                .build();
        return Result.success(userLoginVO);
    }
    @ApiOperation("用户注册接口")
    @PostMapping("/register")
    public Result register(@RequestBody UserDTO userDTO){
        return userService.register(userDTO);
    }
    @ApiOperation("用户退出接口")
    @PostMapping("/logout")
    public Result logout(){
        return Result.success();
    }

    @ApiOperation("用户查询接口")
    @PostMapping("/page")
    public Result<PageResult> page(@RequestBody UserPageQueryDTO userPageQueryDTO){
        log.info("用户查询: {}", userPageQueryDTO);
        PageResult pageResult = userService.pageQuery(userPageQueryDTO);
        return Result.success(pageResult);
    }
    @ApiOperation("用户修改状态")
    @PutMapping("/{id}/{status}")
    public Result updateByIdStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status){
        log.info("用户修改状态: {},{}", id, status);
        userService.updateByIdStatus(id,status);
        return Result.success(MessageConstant.UPDATE_SUCCESS);
    }
    @ApiOperation("用户删除接口")
    @DeleteMapping
    public Result DeleteById(@RequestParam List<Long> ids){
        log.info("ids信息，{}",ids);
        userService.DeleteById(ids);
        return Result.success(MessageConstant.DELETE_SUCCESS);
    }

    @ApiOperation("用户忘记密码")
    @PostMapping("/forgot")
    public Result forgot(@RequestBody UserForgotDTO userForgotDTO){
        userService.updateByUsername(userForgotDTO);
        return Result.success(MessageConstant.UPDATE_SUCCESS);
    }
    @ApiOperation("用户忘记密码")
    @PostMapping("/updateRole")
    public Result update(@RequestBody UserDTO userDTO){
        log.info("用户修改角色: {}", userDTO);
        userService.updateById(userDTO);
        return Result.success(MessageConstant.UPDATE_SUCCESS);
    }

    @ApiOperation("根据用户id查询用户")
    @GetMapping("/{id}")
    public Result<UserInfoVO> getById(@PathVariable Long id){
        log.info("根据用户id查询用户: {}", id);
        UserInfoVO userInfoVO = userService.getById(id);
        return Result.success(userInfoVO);
    }

}
